package services

import (
	"game-server/app/common/request"
	"game-server/app/common/response"
	"game-server/app/models"
	"game-server/global"
)

type ArticleService struct {
}

var ArticleServiceApp = new(ArticleService)

// ArticleList 文章列表
func (s *ArticleService) GetList(params request.ArticleListRequest) (response.ListResponse[models.Article], error) {
	var res response.ListResponse[models.Article]
	// var article models.Article
	query := global.App.DB.Model(&models.Article{})
	if params.Title != "" {
		query = query.Where("title LIKE ?", "%"+params.Title+"%")
	}
	if params.Desc != "" {
		query = query.Where("desc LIKE?", "%"+params.Desc+"%")
	}

	// 统计总数
	err := query.Count(&res.Total).Error
	// 分页查询
	offset := (params.Page - 1) * params.PageSize
	query.Offset(offset).
		Limit(params.PageSize).
		Order("created_at desc").
		Find(&res.List)

	return res, err
}

// ArticleAdd 文章添加
func (s *ArticleService) Add(params request.ArticleAddRequest) error {
	var article models.Article
	article.Title = params.Title
	article.Content = params.Content
	article.Desc = params.Desc
	return global.App.DB.Create(&article).Error
}

// ArticleUpdate 文章更新
func (s *ArticleService) Update(params request.ArticleUpdateRequest) error {
	var article models.Article

	// 先查询文章是否存在
	if err := global.App.DB.First(&article, params.Id).Error; err != nil {
		return err
	}

	// 更新指定字段
	article.Title = params.Title
	article.Content = params.Content
	article.Img = params.Img
	article.Status = params.Status
	article.GameId = params.GameId
	article.Type = params.Type

	return global.App.DB.Model(&article).Updates(map[string]interface{}{
		"title":   params.Title,
		"content": params.Content,
		"img":     params.Img,
		"status":  params.Status,
		"game_id": params.GameId,
		"type":    params.Type,
	}).Error
}

// ArticleDelete 文章删除
func (s *ArticleService) Delete(form request.ArticleDeleteRequest) error {
	var article models.Article
	// 先查询文章是否存在
	if err := global.App.DB.First(&article, form.Id).Error; err != nil {
		return err
	}
	//删除评论
	global.App.DB.Where("article_id = ?", form.Id).Delete(&models.ArticleComment{})
	return global.App.DB.Delete(&article).Error
}

// ArticleDetail 文章详情
func (s *ArticleService) Detail(params request.ArticleDetailRequest) (models.Article, error) {
	var article models.Article
	if err := global.App.DB.First(&article, params.Id).Error; err != nil {
		return article, err
	}

	// 查询顶层评论（parent_id = 0）
	// article.CommentList = s.GetArticleComments(params.Id, 0)
	//返回所有评论 不做递归
	global.App.DB.Where("article_id =?", params.Id).
		Order("created_at desc").
		Find(&article.CommentList)
	return article, nil
}

// GetArticleComments 递归获取文章评论
func (s *ArticleService) GetArticleComments(articleId uint, parentId uint) []models.ArticleComment {
	var comments []models.ArticleComment

	// 查询当前层级的评论
	global.App.DB.Where("article_id = ? AND parent_id = ?", articleId, parentId).
		Order("created_at desc").
		Find(&comments)

	// 递归查询每个评论的子评论
	for i := range comments {
		comments[i].CommentList = s.GetArticleComments(articleId, uint(comments[i].ID.ID))
	}

	return comments
}

// CommentAdd 评论添加
func (s *ArticleService) CommentAdd(params request.ArticleCommentAddRequest) error {
	var comment models.ArticleComment
	comment.ArticleId = params.ArticleId
	comment.ParentId = params.ParentId
	comment.Content = params.Content
	comment.UserId = params.UserId
	return global.App.DB.Create(&comment).Error
}

// CommentUpdate 评论更新
func (s *ArticleService) CommentUpdate(params request.ArticleCommentUpdateRequest) error {
	var comment models.ArticleComment
	// 先查询评论是否存在
	if err := global.App.DB.First(&comment, params.Id).Error; err != nil {
		return err
	}
	//全部字段更新
	comment.Content = params.Content
	return global.App.DB.Save(&comment).Error
}

// CommentDelete 评论删除
func (s *ArticleService) CommentDelete(params request.ArticleCommentDeleteRequest) error {
	var comment models.ArticleComment
	// 先查询评论是否存在
	if err := global.App.DB.First(&comment, params.Id).Error; err != nil {
		return err
	}
	return global.App.DB.Delete(&comment).Error
}

// Random 随机文章
func (s *ArticleService) Random(params request.ArticleRandomRequest) ([]models.Article, error, int64) {
	//
	//根据params的type和数量随机获取文章列表list 和 文字的总条数 返回list和总数
	//计算总数
	var count int64
	global.App.DB.Model(&models.Article{}).Where("type =?", params.Type).Count(&count)

	var list []models.Article
	err := global.App.DB.Where("type =?", params.Type).Order("RAND()").Limit(params.Shuliang).Find(&list).Error
	return list, err, count
}
